package com.ruoyi.userManage.controller;

import cn.hutool.core.util.StrUtil;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.userManage.domain.User;
import com.ruoyi.userManage.domain.UserAddress;
import com.ruoyi.userManage.mapper.UserMapper;
import com.ruoyi.userManage.service.IUserAddressService;
import jakarta.annotation.Resource;
import jakarta.servlet.http.HttpServletResponse;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.util.CollectionUtils;
import org.springframework.util.ObjectUtils;
import org.springframework.web.bind.annotation.*;

import java.util.Collections;
import java.util.List;

/**
 * 地址信息Controller
 *
 * @author ruoyi
 * @date 2025-08-12
 */
@RestController
@RequestMapping("/userAddress/userAddress")
public class UserAddressController extends BaseController {
    @Autowired
    private IUserAddressService userAddressService;
    @Resource
    private UserMapper userMapper;

    /**
     * 查询地址信息列表
     */
    @PreAuthorize("@ss.hasPermi('userAddress:userAddress:list')")
    @GetMapping("/list")
    public TableDataInfo list(UserAddress userAddress) {
        if (!StrUtil.isEmpty(userAddress.getUserPhone())) {
            Long userId = userMapper.selectIdByPhone(userAddress.getUserPhone());
            if (userId != null) {
                userAddress.setUserId(userId);
            } else {
                return getDataTable(Collections.emptyList());
            }
        }

        if (userAddress.getSixUserId() != null ) {
            Long userId = userMapper.selectUserIdBySixUserId(userAddress.getSixUserId());
            if (userId != null) {
                userAddress.setUserId(userId);
            } else {
                return getDataTable(Collections.emptyList());
            }
        }
        startPage();
        List<UserAddress> list = userAddressService.selectUserAddressList(userAddress);
        if (!CollectionUtils.isEmpty(list)) {
            for (UserAddress address : list) {
                String phone = userMapper.selectPhoneById(address.getUserId());
                address.setUserPhone(!StrUtil.isEmpty(phone) ? phone : "");
                User user = userMapper.selectUserById(address.getUserId());
                address.setUserId(!ObjectUtils.isEmpty(user) ? user.getUserId() : 0);
            }
        }
        return getDataTable(list);
    }

    /**
     * 导出地址信息列表
     */
    @PreAuthorize("@ss.hasPermi('userAddress:userAddress:export')")
    @Log(title = "地址信息", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, UserAddress userAddress) {
        if (!StrUtil.isEmpty(userAddress.getUserPhone())) {
            Long userId = userMapper.selectIdByPhone(userAddress.getUserPhone());
            if (userId != null) {
                userAddress.setUserId(userId);
            }
        }

        if (userAddress.getSixUserId() != null ) {
            Long userId = userMapper.selectUserIdBySixUserId(userAddress.getSixUserId());
            if (userId != null) {
                userAddress.setUserId(userId);
            }
        }
        List<UserAddress> list = userAddressService.selectUserAddressList(userAddress);
        if (!CollectionUtils.isEmpty(list)) {
            for (UserAddress address : list) {
                String phone = userMapper.selectPhoneById(address.getUserId());
                address.setUserPhone(!StrUtil.isEmpty(phone) ? phone : "");
            }
        }
        ExcelUtil<UserAddress> util = new ExcelUtil<UserAddress>(UserAddress.class);
        util.exportExcel(response, list, "地址信息数据");
    }

    /**
     * 获取地址信息详细信息
     */
    @PreAuthorize("@ss.hasPermi('userAddress:userAddress:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id) {
        return success(userAddressService.selectUserAddressById(id));
    }

    /**
     * 新增地址信息
     */
    @PreAuthorize("@ss.hasPermi('userAddress:userAddress:add')")
    @Log(title = "地址信息", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody UserAddress userAddress) {
        return toAjax(userAddressService.insertUserAddress(userAddress));
    }

    /**
     * 修改地址信息
     */
    @PreAuthorize("@ss.hasPermi('userAddress:userAddress:edit')")
    @Log(title = "地址信息", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody UserAddress userAddress) {
        return toAjax(userAddressService.updateUserAddress(userAddress));
    }

    /**
     * 删除地址信息
     */
    @PreAuthorize("@ss.hasPermi('userAddress:userAddress:remove')")
    @Log(title = "地址信息", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids) {
        return toAjax(userAddressService.deleteUserAddressByIds(ids));
    }
}
